package messages;

import java.io.IOException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;

import utils.Serialization;
import utils.XmlSerializationUtils;
import connection.exception.ConnectionResetException;

public class DocumentSigningResponseMessage implements Convertible {

    private static final long serialVersionUID = 1L;
    
    /**
     * Boolean, representing whether or not the operation was a success
     */
    public Boolean _result;
    
    /**
     * The Signature structure itself
     */
    public String _signatureStruct; 
    
    /**
     *  The Signature of the Structure below 
     */
    public String _docSignature;

    public DocumentSigningResponseMessage() {}
     
    public DocumentSigningResponseMessage(Boolean success, String sigStruct, String docSignature) {
        _result = success;
        _signatureStruct = sigStruct;
        _docSignature = docSignature;
    }
    
    @Override
    public Element convertToCanonic(Document doc) {
        
        String className = getClass().getSimpleName();
        
        Element el = doc.createElement(className);

        if (_result != null) {
            Element childEl = doc.createElement("Result");
            childEl.setTextContent(_result.toString());
            el.appendChild(childEl);

        }
        
        if (_docSignature != null) {
            Element childEl = doc.createElement("DocSignature");
            childEl.setTextContent(_docSignature);
            el.appendChild(childEl);

        }
                
        if (_signatureStruct != null) {
            Element childEl = doc.createElement("SignatureStruct");
            childEl.setTextContent(_signatureStruct);
            el.appendChild(childEl);

        }

        return el;
    } 


    @Override
    public Object convertFromCanonic(Element el) {
        this._result = new Boolean(XmlSerializationUtils.getTextValue(el, "Result"));
        this._docSignature = XmlSerializationUtils.getTextValue(el, "DocSignature");
        this._signatureStruct = XmlSerializationUtils.getTextValue(el, "SignatureStruct");
        return this;
    }

    public static void main(String[] args) throws ConnectionResetException, IOException{

        DocumentSigningResponseMessage gen = new DocumentSigningResponseMessage();
//        gen._docSignature = "SFDAFECcasdasdas";
//        gen._signatureStruct = "DSADASDASDA" + "Woopi" + "PresidenteIST" + false + "23/04/2012" + 32;
        gen._result = false;
        gen._docSignature = "";
        gen._signatureStruct = "";
        

        byte[] serialized = Serialization.Serialize(gen);

        Object newGen = Serialization.Deserialize(serialized);

        newGen.toString();

        System.in.read();
    }
}

